// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PrettyPrinter.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Usings

    using System;
    using System.Collections.Generic;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The pretty printer.
    /// </summary>
    public class PrettyPrinter
    {
        /// <summary>
        /// The configuration.
        /// </summary>
        public class Configuration
        {
            #region Public Methods

            /// <summary>
            /// The pretty print.
            /// </summary>
            /// <param name="map">
            /// The map.
            /// </param>
            /// <returns>
            /// The pretty print.
            /// </returns>
            public static string PrettyPrint(IConfiguration map)
            {
                var s = string.Empty;

                foreach (var k in map.ConfigurationSchema.Fields)
                {
                    s += String.Format("{0}={1}\n", k, map[k]);
                }

                return s;
            }

            #endregion
        }

        /// <summary>
        /// The configuration field status.
        /// </summary>
        public class ConfigurationFieldStatus
        {
            #region Public Methods

            /// <summary>
            /// The decode enum.
            /// </summary>
            /// <param name="f">
            /// The f.
            /// </param>
            /// <returns>
            /// The decode enum.
            /// </returns>
            public static string DecodeEnum(EConfigurationFieldStatus f)
            {
                var decode = string.Empty;
                if ((f & EConfigurationFieldStatus.Error) != EConfigurationFieldStatus.Error)
                {
                    decode += "Error|";
                }

                if ((f & EConfigurationFieldStatus.ErrorBadType) != EConfigurationFieldStatus.Error)
                {
                    decode += "ErrorBadType|";
                }

                if ((f & EConfigurationFieldStatus.ErrorBadValue) != EConfigurationFieldStatus.Error)
                {
                    decode += "ErrorBadValue|";
                }

                if ((f & EConfigurationFieldStatus.ErrorNullValue) != EConfigurationFieldStatus.Error)
                {
                    decode += "ErrorNullValue|";
                }

                return decode.TrimEnd(new[] { '|' });
            }

            #endregion
        }

        /// <summary>
        /// The strings.
        /// </summary>
        public class Strings
        {
            #region Public Methods

            /// <summary>
            /// Cut some string in slices of given size, removing splaces and blanks a the beginning on slices if present
            /// This is to help formatting long line of logtext to remain quite readable
            /// </summary>
            /// <param name="source">
            /// </param>
            /// <param name="size">
            /// </param>
            /// <returns>
            /// </returns>
            public static string[] Slice(string source, int size)
            {
                var list = new List<string>();
                var i = 0;
                while (i < source.Length)
                {
                    var len = size;
                    if (i + len > source.Length)
                    {
                        len = source.Length - i;
                    }

                    var sliceI = source.Substring(i, len);
                    sliceI = sliceI.TrimStart(new[] { ' ', '\n', '\t' });
                    list.Add(sliceI);

                    i += len;
                }

                return list.ToArray();
            }

            /// <summary>
            /// Reformat a string to be no more than size characters long
            /// </summary>
            /// <param name="source">
            /// </param>
            /// <param name="size">
            /// </param>
            /// <returns>
            /// The slice 1.
            /// </returns>
            public static string Slice1(string source, int size)
            {
                var s = string.Empty;
                foreach (var slice in Slice(source, size))
                {
                    s += slice + "\n";
                }

                return s;
            }

            #endregion
        }

        /// <summary>
        /// The timings.
        /// </summary>
        public class Timings
        {
            #region Constants and Fields

            /// <summary>
            /// The time format.
            /// </summary>
            private static readonly string TimeFormat = "HH:mm:ss.ffffff";

            #endregion

            #region Public Methods

            /// <summary>
            /// The format.
            /// </summary>
            /// <param name="r">
            /// The r.
            /// </param>
            /// <returns>
            /// The format.
            /// </returns>
            public static string Format(DateTime r)
            {
                if (r.Equals(DateTime.MinValue))
                {
                    return "-";
                }

                return r.ToString(TimeFormat);
            }

            #endregion
        }

        /// <summary>
        /// The types.
        /// </summary>
        public class Types
        {
            #region Constants and Fields

            /// <summary>
            /// The full.
            /// </summary>
            private static bool full;

            #endregion

            #region Properties

            /// <summary>
            /// Gets or sets a value indicating whether OptionDisplayFullTypeNames.
            /// </summary>
            public static bool OptionDisplayFullTypeNames
            {
                get
                {
                    return full;
                }

                set
                {
                    full = value;
                }
            }

            #endregion

            // public static string PrettyPrint(string t)
            // {
            // Type tt = applicationContainer.Activator.FindType(t);
            // return PrettyPrint(tt);
            // }
            #region Public Methods

            /// <summary>
            /// The pretty print.
            /// </summary>
            /// <param name="t">
            /// The t.
            /// </param>
            /// <returns>
            /// The pretty print.
            /// </returns>
            public static string PrettyPrint(Type t)
            {
                if (t == null)
                {
                    return "null";
                }

                if (full)
                {
                    return t.FullName;
                }
                else
                {
                    return t.Name;
                }
            }

            /// <summary>
            /// The pretty print.
            /// </summary>
            /// <param name="types">
            /// The types.
            /// </param>
            /// <returns>
            /// The pretty print.
            /// </returns>
            public static string PrettyPrint(Type[] types)
            {
                var s = string.Empty;
                foreach (var t in types)
                {
                    if (full)
                    {
                        s = s + t.FullName;
                    }
                    else
                    {
                        s = s + t.Name;
                    }

                    s = s + ",";
                }

                s = s.TrimEnd(new[] { ',' });
                return s;
            }

            #endregion
        }
    }
}